package jrobutt.room.menu.levelselection;

import jrobutt.*;
import jrobutt.room.menu.MenuItem;
import jrobutt.room.menu.titlescreen.TitleScreenRoom;
import jrobutt.room.level.LevelRoom;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
import java.nio.*;

public class LevelSelectionRoom extends GameRoom
{
    ArrayList<MenuItem> menu_items;
    
    protected Collection<? extends KeyboardResponder> getKeyboardResponders ()
    {
        return menu_items;
    }
    protected Collection<? extends Renderable> getRenderables ()
    {
        return menu_items;
    }
    protected Collection<? extends Updatable> getUpdatables () { return null; }
    
    public LevelSelectionRoom ()
    {
        menu_items = new ArrayList<MenuItem>(4);
    }
    
    public Color get_bg_color ()
    {
        return Settings.bgcolor;
    }
    
    public void enter ()
    {
        int num_levels = ByteBuffer.wrap(Global.bytes_loader.load("levels/length")).getInt();
        Global.num_levels = num_levels;
        int current_level = Global.current_level;
        MenuItem[] temp_menu_items = new MenuItem[num_levels];
        
        float root_x = 128.0f;
        float root_y = 64.0f;
        float x = root_x;
        float y = root_y;
        for (int k=0; k<num_levels; k++)
        {
            GameSprite sprite_selected;
            GameSprite sprite;
            
            if (k <= current_level)
            {
                String image_fname_selected = "levels/" + k + "-menu_item-selected.png";
                String image_fname = "levels/" + k + "-menu_item.png";
                sprite_selected = new GameSprite(Global.image_loader.load(image_fname_selected), 0.5f, 0.5f);
                sprite = new GameSprite(Global.image_loader.load(image_fname), 0.5f, 0.5f);
            }
            else
            {
                String image_fname = "levels/" + k + "-menu_item-disabled.png";
                sprite_selected = null;
                sprite = new GameSprite(Global.image_loader.load(image_fname), 0.5f, 0.5f);
            }
            
            temp_menu_items[k] = new MenuItem(x, y, false, sprite_selected, sprite, null, null, null, null);
            y += sprite.height;
        }
        
        for (int k=0; k<temp_menu_items.length; k++)
        {
            MenuItem menu_item = temp_menu_items[k];
            int prev_index;
            if (k == 0)
            {
                prev_index = current_level;
            }
            else
            {
                prev_index = k - 1;
            }
            menu_item.up = temp_menu_items[prev_index];
            
            int next_index = 0;
            if (k == current_level)
            {
                next_index = 0;
            }
            else
            {
                next_index = (k + 1) % temp_menu_items.length;
            }
            menu_item.down = temp_menu_items[next_index];
            menu_items.add(menu_item);
        }
        temp_menu_items[0].is_selected = true;
    }
    public void exit ()
    {
        menu_items.clear();
    }
    
    public GameRoom key_pressed (int key)
    {
        if (key == KeyEvent.VK_ESCAPE)
        {
            return new TitleScreenRoom();
        }
        
        if (key == KeyEvent.VK_ENTER)
        {
            int level = 0;
            for (MenuItem menu_item : menu_items)
            {
                if (menu_item.is_selected)
                {
                    return new LevelRoom(level);
                }
                level++;
            }
        }
        
        for (MenuItem menu_item : menu_items)
        {
            if (!menu_item.is_selected) continue;
            
            menu_item.key_pressed(key);
            break;
        }
        return null;
    }
    
    public GameRoom update ()
    {
        return null;
    }
}
